package com.awesome.kotlindemo.base

import com.awesome.common.util.log.ShowLogUtil
import java.lang.Exception

/**
 * Author: JfangZ
 * Email: zhangjingfang@jeejio.com
 * Date: 2021/4/8 16:48
 * Description:基础语言
 */
class BaseLanguage {
    // 延迟属性
    val p: String by lazy{
        // 计算该字符串
        "str"
    }

    // 关键字val相当于final
    val a: Int = 1 //立即赋值
    val b = 2//自动推断出Int类型
//    val c: Int // 错误写法，赋值初始值不可以省略
//    c = 3// 赋值

    // 关键字var为变量
    var x = 5

    fun main() {
        ShowLogUtil.info(add(1, 2));
        ShowLogUtil.info(sum(2, 2));
        printSum(3, 2)
        x += 1
        ShowLogUtil.info("x = $x")
        if (a > x) {
            ShowLogUtil.info("$a > $x")
        } else {
            ShowLogUtil.info("$a < $x")
        }
        ShowLogUtil.info(getStringLength("123"))

        // 遍历区间
        for (x in 1..5)
            ShowLogUtil.info(x)

        // 遍历数列
        for (x in 1..10 step 2)
            ShowLogUtil.info(x)

        for (x in 9 downTo 0 step 3)
            ShowLogUtil.info(x)

        /**
         * list集合
         */
        // 创建集合
        val dataList = listOf("kiwifruit", "blueberry", "watermelon")

        // 关键字in和符号..配合判断是否在指定区间
        if (-1 !in 0..dataList.lastIndex)
            ShowLogUtil.info("-1 is out of range ${dataList.lastIndex}")

        if (dataList.size !in dataList.indices)
            ShowLogUtil.info("list size is ${dataList.size} out of valid list indices range ${dataList.indices}, too")


        // foreach循环，关键字for，in
        for (data in dataList) {
            ShowLogUtil.info("$data")
        }
        
        // fori循环，关键字for，in
        for (index in dataList.indices) {
            ShowLogUtil.info("$index I like ${dataList[index]}")
        }
        
        // while循环
        var index = 0
        while (index < dataList.size) {
            ShowLogUtil.info("data at $index is ${dataList[index]}")
            index++
        }

        // when
        ShowLogUtil.info(describe(1))
        ShowLogUtil.info(describe("Hello"))
        ShowLogUtil.info(describe(3452345234523452345))
        ShowLogUtil.info(describe(2))

        // 运算符in判断集合内是否包含某实例
        when {
            "orange" in dataList -> ShowLogUtil.info("juicy")
            "kiwifruit" in dataList -> ShowLogUtil.info("kiwifruit has lots of VC")
        }

        // filter
        dataList
            .filter { it.contains("u") }//过滤
            .sortedBy { it }//排序
            .map { it.toUpperCase() }//操作
            .forEach { ShowLogUtil.info(it) }//foreach循环

        /**
         * map集合
         */
        // 创建一个map
        val map = mapOf("a" to 1, "b" to 2, "c" to 3)
        // 遍历map
        for ((k, v) in map)
            ShowLogUtil.info("$k $v")
        // 根据key获取map的value
        ShowLogUtil.info(map["a"])

        // ?""三目运算符
        val string = getString(-1)
        ShowLogUtil.info(string?.length ?: "empty")
    }


    // 定义函数格式
    // 关键字fun 函数名(参数名1： 参数类型，参数名2： 参数类型)： 返回类型{
    //      return 值
    // }
    fun add(a: Int, b: Int): Int {
        return a + b
    }

    // 表达式作为函数体，返回值类型自动类型推断
    fun sum(a: Int, b: Int) = a + b

    // Unit类型无意义可以不用返回
    fun printSum(a: Int, b: Int): Unit {
        println("jlog sum of $a and $b is ${a + b}")
    }

    // 关键字is类似instanceof，使用完成即强转
    fun getStringLength(obj: Any): Int? {
        if (obj is String)
            return obj.length
        return null
    }

    // when为switch case语句
    fun describe(obj: Any): String =
        when (obj) {
            1 -> "one"
            "Hello" -> "Greeting"
            is Long -> "Long"
            else -> "Unknown"
        }

    fun getList() {
        val dataList = mutableListOf<String>("1", "b")
        ShowLogUtil.info("dataList.size=${dataList.size}")
    }

    fun tryCatch() {
        val result = try {
            getString(1)
        } catch (e: Exception){
            e.printStackTrace()
        }
    }

    fun getString(a: Int): String? {
        if (a > 0)
            return "a is valid number"
        return null
    }
}